from PyQt4.QtCore import SIGNAL, QObject
from core import cmds
from ui.graph import GraphView

class Graph(ni.View):
    pipeline = ni.Reference(type=ni.Pipeline)
    
    widget = GraphView
    
    def setup_widget(self, widget):
        QObject.connect(widget.scene(), SIGNAL("selectionChanged()"), 
                        lambda w=widget: self.graph_selection_changed(w))
    
    def update_widget(self, widget):
        widget.set_pipeline(self.pipeline)
        
    def graph_selection_changed(self, widget):
        comp = widget.scene().current_component()
        if comp is None: return
        cmds.select(comp)
        
    def current_changed(self, component):
        if isinstance(component, ni.Pipeline):
            self.pipeline = component
            self.update_widgets()